package com.haojii.helloworld;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class ContactServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		List<Contact> contactList = ContactUtils.list();
		if(contactList.size()==0)
		{
			for (int i = 0; i < 2; i++) {
				Contact c = new Contact(new Date(), "firstname"+i, "lastname"+i, "email"+i+"@gmail.com", "137xxxxxxx"+i);
				ContactUtils.save(c);
				contactList.add(c);
			}
		}
		req.setAttribute("contactList", contactList);
		req.getRequestDispatcher("contact.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String method = req.getParameter("method");
		String id = req.getParameter("id");
		String gravatarURL = req.getParameter("gravatarURL");
		String lastname = req.getParameter("lastname");
		String firstname = req.getParameter("firstname");
		String birthday = req.getParameter("birthday");
		String mobilephone = req.getParameter("mobilephone");
		String email = req.getParameter("email");
		
		System.out.println("method="+method + "; Contact " + "[id=" + id + ", birthday=" + birthday + ", email=" + email
				+ ", firstname=" + firstname + ", lastname="
				+ lastname + ", mobilephone=" + mobilephone + ", gravatarURL=" + gravatarURL + "]");
		
		Date d = null;
		try {
			if(birthday != null) {
				d = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
			}
		} catch (ParseException e) {
			//just ignore
		}
		
		boolean result = false;
		String msg = "";
		if ((id == null)||(id!=null && id.isEmpty())) // create request
		{
			
			Contact c = new Contact(d,firstname,lastname,email,mobilephone);
			result = ContactUtils.save(c);
			msg = "\"contact\":" + c.asJSON();
		}
		else if(method != null && method.equals("delete")) //delete request
		{
			result = ContactUtils.delete(Long.parseLong(id));
		}
		else // update request
		{
			Contact c = new Contact(d,firstname,lastname,email,mobilephone);
			c.setId(Long.parseLong(id));
			result = ContactUtils.update(c);
			msg = "\"contact\":" + c.asJSON();
		}
		
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("application/json");
		resp.setHeader("pragma", "no-cache");
		resp.setHeader("expires", "Thu, 29 Nov 2007 16:18:42 GMT");
		resp.getWriter().print("{");
		resp.getWriter().print("\"success\":\"" + result + "\",");
		resp.getWriter().print(msg);
		resp.getWriter().print("}");
		resp.getWriter().flush();
		resp.getWriter().close();
		//resp.flushBuffer();
		
	}
}
